﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>机构启用应用</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link href="/lobsteruiframe/layuiadmin/layui/css/layui.css" rel="stylesheet" />
    <link href="/lobsteruiframe/layuiadmin/style/admin.css" rel="stylesheet" />
    <script src="/lobsteruiframe/layuiadmin/layui/layui.js"></script>
    <script src="/jslib/jquery-3.4.1.min.js"></script>
    <script src="/jslib/lobster.js"></script>
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header  layuiadmin-card-header-auto">
                <div class="layui-input-inline">
                    <div class="layui-form-item">
                        <label class="layui-form-label">机构：</label>
                        <div class="layui-input-block">
                            <select name="workId" id="workId" lay-filter="workId"></select>
                        </div>
                    </div>
                </div>
                <div class="layui-input-inline">
                    <div class="layui-form-item">
                        <button class="layui-btn layuiadmin-btn-useradmin" lay-filter="btnAddApp" id="btnAddApp">
                            <i class="layui-icon layuiadmin-button-btn layui-icon-add-1"></i>启用新应用
                        </button>
                    </div>
                </div>
                <div class="layui-input-inline">
                    <div class="layui-form-item">
                        <button class="layui-btn layuiadmin-btn-useradmin" lay-filter="btnUpgradeApp" id="btnUpgradeApp">
                            升级所有应用前端代码
                        </button>
                    </div>
                </div>
                <div class="layui-input-inline">
                    <div class="layui-form-item">
                        <button class="layui-btn layuiadmin-btn-useradmin" lay-filter="btnSyncApp" id="btnSyncApp">
                            同步所有应用后端代码
                        </button>
                    </div>
                </div>
                <div class="layui-input-inline">
                    <div class="layui-form-item">
                        <button class="layui-btn layuiadmin-btn-useradmin" lay-filter="btnClearCacheApp" id="btnClearCacheApp">
                            清空缓存
                        </button>
                    </div>
                </div>
            </div>
            <div class="layui-card-body">
                <table id="LAY-apptable" lay-filter="LAY-apptable"></table>

            </div>
        </div>
    </div>
</body>
</html>

<script type="text/javascript">
    lobsterlayui.main(['index', 'layer', 'form', 'element','multiSelect','table'], {
        data: {
            $layui: null,
            right: null,
            tableapp: null,
            tablebaseapp: null,
            tableemp: null,
        },
        pageload: function (right) {
            this.data.right = right;
            this.loadselectworker(() => {
                this.data.tableapp = this.loadtableapp();
            });
            
            this.initevent();
        },
        initevent() {
            var self = this;
            // if (self.data.right.IsAdmin == 1) {
            //     $("#workId").attr("disabled", "");
            // }
            //监听机构下拉框
            layui.form.on('select(workId)', function (data) {
                self.data.tableapp.reload({
                    where: {
                        workid: $("#workId").val()
                    }
                    , page: {
                        curr: 1 //重新从第 1 页开始    
                    }
                });
            });

            //添加应用  
            $('#btnAddApp').on('click', function () {
                layer.open({
                    type: 1
                    , title: '添加应用'
                    , content: $('#dlg_addapp').html()
                    , maxmin: true
                    , area: ['680px', '620px']  //宽，高
                    , cancel: function () {

                    }
                    , success: function (layero, index) {
                        //添加应用
                        layui.form.on("submit(LAY-app-front-search)", function (data) {
                            sel.data.tablebaseapp.reload({
                                where: {
                                    workId: data.field.WorkId,
                                    keywords: data.field.AppName
                                },
                                page: {
                                    curr: 1
                                }
                            });
                        });
                        //监听行工具事件
                        layui.table.on('tool(BaseAppTable)', function (obj) {
                            var data = obj.data;//当前行数据
                            if (obj.event === "install") {
                                self.InstallApp(data);
                            }
                        });

                        $('#addapp_workId').val($("#workId").val());
                        self.data.tablebaseapp= self.loadtableaddapp($("#workId").val());

                    }
                    , yes: function (index, layero) {

                    }
                });
            });

            //升级应用  
            $('#btnUpgradeApp').on('click', function () {
                
                layer.confirm("你是否升级所有应用？", { icon: 3, title: "提示" },function (index) {
                    var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                    lobsterlayui.GET('/UpgradeApp/?workid='+$("#workId").val()).then(retdata=>{
                        layer.close(load); //关闭弹层
                        layer.close(index); //关闭弹层
                        layer.msg("升级成功！");
                    }).catch(res=>{
                        layer.msg(res.msg);
                    })
                })
            });

            //升级应用  
            $('#btnSyncApp').on('click', function () {

                layer.confirm("你是否同步所有应用？", { icon: 3, title: "提示" }, function (index) {
                    var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                    lobsterlayui.GET('/admin/v1/worker/SyncApp_formApple', { workId: $("#workId").val() }).then(retdata => {
                        layer.close(load); //关闭弹层
                        layer.close(index); //关闭弹层
                        layer.msg("同步成功！");

                        //lobsterlayui.GET('/mirth/v1/LobsterCLI/ClearLobsterRouteCache').then((data) => {
                        //    layer.msg("清理应用缓存成功");
                        //});

                    }).catch(res => {
                        layer.msg(res.msg);
                    })
                })
            });
            
            //清空缓存  
            $('#btnClearCacheApp').on('click', function () {

                layer.confirm("你是否清理应用缓存？", { icon: 3, title: "提示" }, function (index) {
                    var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                    lobsterlayui.GET('/mirth/v1/LobsterCLI/ClearLobsterRouteCache').then((data) => {
                        layer.close(load);
                        layer.msg("清理应用缓存成功");
                    });
                })
            });
            //监听行工具栏事件（编辑、启停用）
            layui.table.on('tool(LAY-apptable)', function (obj) {
                //LAY-system-parameter是lay-filter值  
                var data = obj.data;//当前行数据     
                if (obj.event === "disable" || obj.event === "enable") {
                    self.stopbaseapp(data);
                }
                else if (obj.event === "auth") {
                    layer.open({
                        type: 1
                        , title: '应用授权人员-' + data.AppName
                        , content: $('#dlg_addemp').html()
                        //, maxmin: true
                        , area: ['800px', '500px']  //宽，高
                        , cancel: function () {

                        }
                        , success: function (layero, index) {
                            //授权用户
                            $('#btnAppEmp').on('click', function () {
                                self.AddEmp();
                            });
                            //监听行工具事件
                            layui.table.on('tool(LAY-empTable)', function (obj) {
                                var data = obj.data;//当前行数据
                                if (obj.event === "delete") {//移除人员
                                    self.DeleteEmp(data);
                                }
                            });

                            $('#addemp_workId').val($("#workId").val());
                            $('#addemp_appId').val(data.AppId);

                            self.data.tableemp = self.loadtableemp();
                            self.loadselectemp();
                            self.loadselectgroup();
                        }
                        , yes: function (index, layero) {

                        }
                    });
                }
                else if (obj.event === "regkey") {
                    //注册码
                    layer.open({
                        type: 1
                        , title: '应用注册码-' + data.AppName
                        , content: $('#dlg_regkey').html()
                        //, maxmin: true
                        ,btn: ["保存", "取消"]
                        , area: ['500px', '300px']  //宽，高
                        , cancel: function () {

                        }
                        , success: function (layero, index) {
                            
                            $('#baseappId').val(data.Id);
                            $('#baseappRegkey').val(data.RegKey);

                        }
                        , yes: function (index, layero) {
                            var submit = $('#btnSubmit_RegKey');
                            layui.form.on('submit(btnSubmit_RegKey)', function (formdata) {
                                lobsterlayui.GET('/admin/v1/worker/SaveBaseAppRegKey', { id: $('#baseappId').val(), regkey: $('#baseappRegkey').val() }).then(data => {
                                    layer.close(index);
                                    layer.msg("保存成功");
                                    self.data.tableapp.reload();
                                })
                            });
                            submit.trigger("click");
                        }
                    });
                }
                else if (obj.event === "updatefront") {
                    layer.confirm("你是否升级应用？", { icon: 3, title: "提示" }, function (index) {
                        var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                        lobsterlayui.GET('/UpgradeWebApp/?appid=' + data.AppId).then(retdata => {
                            layer.close(load); //关闭弹层
                            layer.close(index); //关闭弹层
                            layer.msg("升级成功！");
                        }).catch(res => {
                            layer.msg(res.msg);
                        })

                        lobsterlayui.GET('/UpgradeWeuiApp/?appid=' + data.AppId).then(retdata => {

                        }).catch(res => {
                            layer.msg(res.msg);
                        })
                    })
                }
                else if (obj.event === "syncback") {
                    layer.confirm("你是否同步应用？", { icon: 3, title: "提示" }, function (index) {
                        var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                        lobsterlayui.GET('/admin/v1/worker/SyncSingleApp_formApple', { appId: data.AppId }).then(retdata => {
                            layer.close(load); //关闭弹层
                            layer.close(index); //关闭弹层
                            layer.msg("同步成功！");

                        }).catch(res => {
                            layer.msg(res.msg);
                        })
                    })
                }
            });
          
        },
        loadselectworker(callback) {
            var self = this;
            var url = '/admin/v1/worker/GetSelectWorkerDataByUser';//默认平台管理员
            if (self.data.right.IsAdmin == 2) {//机构管理员
                url = '/admin/v1/worker/GetSelectWorkerData';
            }
            lobsterlayui.GET(url).then(data => {
                var workers = data["Workers"];
                $("#workId").html("");
                $.each(workers, function (i, item) {
                    $("#workId").append("<option value=\"" + item.WorkId + "\">" + item.WorkName + "</option>");
                });

                $('#workId').val(self.data.right.WorkId);//默认选择当前登录机构
                //重新渲染
                layui.form.render("select");
                if (callback) {
                    callback();
                }
            })
        },
        loadtableapp() {
            var table = layui.table;
            return table.render({
                elem: '#LAY-apptable'
                , headers: lobsterlayui.GetHeaderData()
                , where: {
                    workId: $("#workId").val() || 0
                }
                , height: 'full-120'
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度
                , url: '/admin/v1/worker/GetBaseAppData' //数据接口
                , page: true //开启分页
                , cols: [[ //表头 
                    { field: 'AppKey', title: '应用标识', width: '200' }
                    , { field: 'AppName', title: '应用名称', width: '200' }
                    , { field: 'AppVersion', title: '版本', width: '100' }
                    , { field: 'CreateUserName', title: '启用者', width: '120' }
                    , { field: 'CreateDate', title: '启用时间', width: '180' }
                    , { field: 'DelFlag', title: '状态标识', width: '120', templet: '#flagTpl' }
                    , { title: '操作', width: '450', align: 'center', fixed: 'right', toolbar: '#bar' }
                ]],
                parseData: function (res) {
                    return {
                        code: res.code,
                        msg: res.msg,
                        count: res.data.count,
                        data: res.data.data
                    }
                }
            });
        },
        stopbaseapp(data) {
            var self = this;
            // 启用停用科室
            var confirm_msg = "";
            var status = 0;
            if (data.DelFlag == 0) {
                confirm_msg = "确认停用么?";
                status = 1;
            }
            else {
                confirm_msg = "确认启用么?";
                status = 0;
            }
            //var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
            layer.confirm(confirm_msg, { icon: 3, title: "提示" },
                function (index) {
                    lobsterlayui.GET('/admin/v1/worker/StopBaseApp', { id: data.Id, status: status }).then(data => {
                        layer.close(index);
                        self.data.tableapp.reload();
                    }).catch(res => {
                        layer.msg(res.msg);
                        layer.close(index);
                    })
                });
        },
        loadtableaddapp(workid) {
            //表格绑定
            return layui.table.render({
                elem: '#BaseAppTable',
                headers: lobsterlayui.GetHeaderData(),
                where: {
                    workId: workid,
                    keywords: ''
                },
                height: 'full',
                cellMinWidth: 80,
                url: '/admin/v1/worker/GetUnInstallAppData_formApple',
                page: true,
                cols: [[
                    { field: 'AppId', title: 'ID', width: '80' },
                    { field: 'AppKey', title: '应用标识', width: '200' },
                    { field: 'AppName', title: '应用名称', width: '200' },
                    { field: 'AppUrl', title: '应用介绍', width: '400' },
                    { title: '操作', width: "100", fixed: 'right', align: 'center', toolbar: '#dlg_addapptableoption' }
                ]],
                parseData: function (res) {
                    return {
                        code: res.code,
                        msg: res.msg,
                        count: res.data.count,
                        data: res.data.data
                    }
                }
            });
        },
        InstallApp(data) {
            var self = this;
            // 启用停用科室
            var confirm_msg = "你确定要安装此应用吗？";

            var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
            layer.confirm(confirm_msg, { icon: 3, title: "提示" },
                function (index) {
                    lobsterlayui.GET('/admin/v1/worker/InstallApp_formApple', { workId: $('#addapp_workId').val(), appId: data.AppId }).then(data => {
                        layer.close(load); //关闭弹层
                        layer.close(index); //关闭弹层
                        self.data.tablebaseapp.reload();//刷新网格
                    })
                });
        },
        loadtableemp() {
            //表格绑定
            return layui.table.render({
                elem: '#empTable',
                headers: lobsterlayui.GetHeaderData(),
                where: {
                    workId: $('#addemp_workId').val(),
                    appId: $('#addemp_appId').val()  //获取文本值
                },
                height: 'full',
                cellMinWidth: 80,
                //size: 'sm',
                //toolbar: '#toolbar',
                url: '/admin/v1/worker/GetAppEmpData',
                page: true,
                //even: true,
                cols: [[
                    { width: "70", field: 'AppUserId', title: 'ID', fixed: 'left' },
                    { width: "100", field: 'EmpName', title: '人员姓名' },
                    { width: "80", field: 'StrSex', title: '性别' },
                    { field: 'IDNumber', title: '身份证号码', width: '200' },
                    { field: 'Telephone', title: '电话', width: '120' },
                    { field: 'DeptName', title: "默认科室", width: "120" },
                    { field: 'UserTypeName', title: "人员类别", width: "100" },
                    { width: "180", field: 'CreateTime', title: '创建日期' },
                    { width: "100", title: '操作', fixed: 'right', align: 'center', toolbar: '#dlg_addemptableoption' }   //操作script的id
                ]],
                parseData: function (res) {
                    return {
                        code: res.code,
                        msg: res.msg,
                        count: res.data.count,
                        data: res.data.data
                    }
                }
            });
        },
        AddEmp() {
            var self = this;
            var emps = [];
            $('#selectemp').find('option:selected').each(function () {
                emps.push($(this).val());
            })

            var groups = [];
            $('#selectgroup').find('option:selected').each(function () {
                groups.push($(this).val());
            })

            if (emps.length == 0) {
                layer.msg('请先选择一个人员进行添加', {
                    offset: '15px'
                    , icon: 1
                    , time: 1000
                });
                return;
            }
            var param = {
                workId: $('#addemp_workId').val(),
                appId: $('#addemp_appId').val(),
                emps: emps.join(","),
                groups: groups.join(",")
            }
            lobsterlayui.GET('/admin/v1/worker/AddEmp', param).then(data => {
                self.data.tableemp.reload();
                self.loadselectemp();
                self.loadselectgroup();
            })
        },
        DeleteEmp(data) {
            var self = this;
            // 启用停用科室
            var confirm_msg = "你确定要删除此人员的授权吗？";

            var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
            layer.confirm(confirm_msg, { icon: 3, title: "提示" },
                function (index) {
                    var param = {
                        workId: $('#addemp_workId').val(),
                        appId: $('#addemp_appId').val(),
                        empId: data.EmpId,
                        appUserId: data.AppUserId
                    }
                    lobsterlayui.GET('/admin/v1/worker/DeleteEmp', param).then(data => {
                        layer.close(load); //关闭弹层
                        layer.close(index); //关闭弹层
                        self.data.tableemp.reload();
                        self.loadselectemp();
                        self.loadselectgroup();
                    })

                });
        },
        loadselectemp() {
            //获取机构数据
            lobsterlayui.GET('/admin/v1/worker/GetAddEmpData', { workId: $('#addemp_workId').val(), appId: $('#addemp_appId').val() }).then(data => {
                //医疗机构
                var empdata = data["data"];
                $("#selectemp").html("");
                $.each(empdata, function (i, item) {
                    $("#selectemp").append("<option value=\"" + item.EmpId + "\">" + item.Name + "</option>");
                });

                //重新渲染
                layui.form.render("select");
                layui.multiSelect.render('select', 'LAY-selectgroup');
            });

        },
        loadselectgroup() {
            //获取机构数据 
            lobsterlayui.GET('/admin/v1/worker/GetAddGroupData', { workId: $('#addemp_workId').val(), appId: $('#addemp_appId').val() }).then(data => {
                //医疗机构
                var groupdata = data["data"];
                $("#selectgroup").html("");
                $.each(groupdata, function (i, item) {
                    $("#selectgroup").append("<option value=\"" + item.GroupId + "\">" + item.Name + "</option>");
                });

                //重新渲染
                layui.form.render("select");
                layui.multiSelect.render('select', 'LAY-selectgroup');
            })
            
        }
    });
</script>

<script type="text/html" id="flagTpl">
    {{#  if(d.DelFlag == '1'){ }}
    <span style="color:red;">停用</span>
    {{#  } else { }}
    <span>正常</span>
    {{#  } }}
</script>
<script type="text/html" id="bar">
    <a class="layui-btn layui-btn-xs" lay-event="auth"><i class="layui-icon layui-icon-auz"></i>授权人员</a>
    <a class="layui-btn layui-btn-xs" lay-event="regkey">注册码</a>
    {{#  if(d.DelFlag == '0'){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="disable"><i class="layui-icon layui-extend-stop"></i>停用</a>
    {{#  } else { }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="enable"><i class="layui-icon layui-icon-ok"></i>启用</a>
    {{#  } }}
    <a class="layui-btn layui-btn-xs" lay-event="updatefront"><i class="layui-icon layui-icon-auz"></i>升级前端</a>
    <a class="layui-btn layui-btn-xs" lay-event="syncback"><i class="layui-icon layui-icon-auz"></i>同步后端</a>
</script>

<script type="text/html" id="dlg_addapp">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-hide">
                    <input type="text" id="addapp_workId" name="WorkId" value="0" />
                </div>
                <div class="layui-input-inline">
                    <label class="layui-form-label">应用名称：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="AppName" placeholder="请输入名称查询" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-input-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-app-front-search" id="btnSearch">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查找
                    </button>
                </div>
            </div>

            <div class="layui-card-body">
                <table id="BaseAppTable" lay-filter="BaseAppTable"></table>
            </div>
        </div>
</script>
<script type="text/html" id="dlg_addapptableoption">
    {{#  if(d.Status == '0'){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="install"><i class="layui-icon layui-extend-unbind"></i>安装</a>
    {{#  } else { }}
    已安装
    {{#  } }}
</script>

<script type="text/html" id="dlg_addemp">
    <div class="layui-card">

        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-input-inline">
                <div class="layui-form-item">
                    <label class="layui-form-label">人员：</label>
                    <div class="layui-input-block">
                        <select multiple="multiple" id="selectemp" name="selectemp" lay-filter="LAY-selectemp"></select>
                    </div>
                </div>
            </div>
            <div class="layui-input-inline">
                <div class="layui-form-item">
                    <label class="layui-form-label">角色：</label>
                    <div class="layui-input-block">
                        <select multiple="multiple" id="selectgroup" name="selectgroup" lay-filter="LAY-selectgroup"></select>
                    </div>
                </div>
            </div>
            <div class="layui-input-inline">
                <div class="layui-hide">
                    <input type="text" id="addemp_workId" name="workId" value="0" />
                    <input type="text" id="addemp_appId" name="appId" value="0" />
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-appemp" id="btnAppEmp">
                        添加
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="empTable" lay-filter="LAY-empTable"></table>
        </div>

    </div>

</script>
<script type="text/html" id="dlg_addemptableoption">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i class="layui-icon layui-extend-unbind"></i>移除</a>
</script>

<script type="text/html" id="dlg_regkey">
    <div class="layui-fluid box">
        <form class="layui-form" lay-filter="formRegKey" id="formRegKey">
            <div class="layui-card">
                <div class="layui-card-body box" id="workdetail">
                    <div class="layui-row">
                        <div class="layui-hide">
                            <input type="text" id="baseappId" value="0" />
                        </div>
                        
                        <div class="layui-col-xs12">
                            <div class="layui-form-item">
                                <label class="layui-form-label">机构注册码</label>
                                <div class="layui-input-block">
                                    <input type="text" name="RegKey" id="baseappRegkey" class="layui-input" style="position:absolute;z-index:2" autocomplete="off">
                                </div>
                            </div>
                        </div>
                       
                    </div>
                    <div class="layui-form-item layui-hide">
                        <input type="button" lay-submit lay-filter="btnSubmit_RegKey" id="btnSubmit_RegKey" value="保存">
                        <input type="reset" id="LAY-baseapp-form-reset" value="重置">
                    </div>
                </div>
            </div>
        </form>
    </div>
</script>